package com.ct.ah.lab.service.impl;

import com.ct.ah.lab.dao.ProductDao;
import com.ct.ah.lab.entity.PageRecord;
import com.ct.ah.lab.entity.Product;
import com.ct.ah.lab.service.IProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

@Service("ProductService")
public class ProductServiceImpl implements IProductService {
    @Autowired
    private ProductDao productDao;

    @Override
    public Product getById(Integer id) {
        Product product = productDao.getById(id);
        return product;
    }

    @Override
    public PageRecord<List<Product>> listAllByCid(Integer cid, Integer currentPage, Integer pageSize) {
        Integer count = productDao.getCountByCid(cid);//获取商品的数量
        Integer pageCount = (count - 1) / pageSize + 1;//计算总页数
        Integer recordIndex = (currentPage - 1) * pageSize;//计算分页查询的起始值
        Map<String,Integer> map = new HashMap<>();
        map.put("cid",cid);
        map.put("recordIndex",recordIndex);
        map.put("pageSize",pageSize);
        List<Product> data = productDao.listAllByCid(map);
        PageRecord pageRecord = new PageRecord(pageSize, count, pageCount, currentPage, data);
        return pageRecord;
    }

    @Override
    public PageRecord<List<Product>> listAllByPname(String pname, Integer currentPage, Integer pageSize) {
        Integer count = productDao.getCountByPname(pname);//获取商品的数量
        Integer pageCount = (count - 1) / pageSize + 1;//计算总页数
        Integer recordIndex = (currentPage - 1) * pageSize;//计算分页查询的起始值
        Map<String,Object> map = new HashMap<>();
        map.put("pname",pname);
        map.put("recordIndex",recordIndex);
        map.put("pageSize",pageSize);
        List<Product> data = productDao.listAllByPname(map);
        PageRecord pageRecord = new PageRecord(pageSize, count, pageCount, currentPage, data);
        return pageRecord;
    }

    @Override
    public PageRecord<List<Product>> listAllByPnames(List<String> nameList, Integer currentPage, Integer pageSize) {
        Integer count = productDao.getCountByPnames(nameList);//获取商品的数量
        Integer pageCount = (count - 1) / pageSize + 1;//计算总页数
        Integer recordIndex = (currentPage - 1) * pageSize;//计算分页查询的起始值
        Map<String,Object> map = new HashMap<>();
        map.put("nameList",nameList);
        map.put("recordIndex",recordIndex);
        map.put("pageSize",pageSize);
        List<Product> data = productDao.listAllByPnames(map);
        PageRecord pageRecord = new PageRecord(pageSize, count, pageCount, currentPage, data);
        return pageRecord;
    }

    @Override
    public List<Product> getRandomProducts() {

        List<Product> list = new ArrayList<>();
        List<Product> all = productDao.listAll();

        for(int i = 0; i < 4; i++){
            int r = (int) (Math.random() * all.size());
            list.add(all.get(r));
            all.remove(r);
        }
        return list;
    }

    @Override
    public List<Product> getHotProducts() {
        List<Product> list = new ArrayList<>();
        List<Product> all = productDao.listAll();

        for(int i = 0; i < 10; i++){
            int r = (int) (Math.random() * all.size());
            list.add(all.get(r));
            all.remove(r);
        }
        return list;
    }
}
